package tichu_project252.Round;

import java.util.LinkedList;
import tichu_project252.CardClasses.Card;
import tichu_project252.Table;

public class Pair extends TurnType {

    /**
     * Returns true if the combination given is Pair.
     *
     * <p> <b> Accessor(Selector): </b> isPair(LinkedList <Card> l) </p>
     *
     * <p><b> Precondition: </b> The player should select some cards.</p> </p>
     * <p> <b> Postcondition:</b> Returns true if the combination is a pair,
     * else it returns false. </p>
     *
     * @return if the combination is pair.
     */
    public static boolean isPair(LinkedList<Card> l, Table t) {
      
        LinkedList<Card> temp = t.getLastDropped(); // the table cards
        
        
        
        if (l.size() == 2) { // to be a pair, we must have 2 cards in our list

            if (l.get(0).getintRepr() == l.get(1).getintRepr()) {  // to be a pair, the 2 cards must have the same int representation
                
                if (temp.size() == 0) { //if the table is empty drop the cards
                    return true;
                } else if (temp.size() == 2) { //if the table has cards
                    if(l.size() != temp.size()) { // the combinations must have the same size
                        return false;
                    }
                    else if ("Phoenix".equals(temp.get(0).getstringRepr())) {//if the previous combination has Phoinix, compare the other card with currentone
                        return t.getLastDropped().get(1).getintRepr() < l.get(1).getintRepr();
                    } else if ("Phoenix".equals(temp.get(1).getstringRepr())) {
                        return t.getLastDropped().get(0).getintRepr() < l.get(0).getintRepr();
                    } else {
                        return t.getLastDropped().get(0).getintRepr() < l.get(0).getintRepr(); //compare if the previous combination is smaller than the curent
                    }

                } else {
                    return false;//if the temp size is different than 2 he havent got pairs to deal with
                }
            } else if ("Phoenix".equals(l.get(0).getstringRepr()) || "Phoenix".equals(l.get(1).getstringRepr())) {
                // the case we have phoenix in our combination 
                if ("Phoenix".equals(l.get(0).getstringRepr())) { // if the phoenix is in the first posiition
                    if (t.getLastDropped().size() == 0) { // if the table is empty
                        return true;
                    } else {
                         return t.getLastDropped().get(0).getintRepr() < l.get(1).getintRepr(); //if the table is not empty compare the other card that user have chose with the previous
                    }
                   
                } else if ("Phoenix".equals(l.get(1).getstringRepr())) { //if the phoenix is in the second position
                    if (t.getLastDropped().size() == 0) { //if the table is empty
                        return true;
                    } else {
                        return temp.get(0).getintRepr() < l.get(0).getintRepr(); ////if the table is not empty compare the  other card with previous
                    }
                }
            }
        }
        return false;  //if the size isn't 2 is not pair
    }

    /**
     * This function is used in full house to check the Pair.
     * 
     * <p> <b> Accessor(Selector): </b> is(LinkedList <Card> l) </p>
     *
     * <p><b> Precondition: </b> The player should select some cards.</p> </p>
     * <p> <b> Postcondition:</b> Returns true if the combination is a pair,
     * else it returns false. </p>
     * 
     * @param l The chosen cards
     * @return  Returns true if the cards has the same int representation else returns false
     */
    public static boolean is(LinkedList<Card> l) {

        if (l.size() == 2) {
            if (l.get(0).getintRepr() == l.get(1).getintRepr()) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

}
